package com.rs.adminweb.schedule.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.AdminBaseAction;
import com.rs.rsbiz.doctor.delegate.DoctorScheduleDelegate;
import com.rs.rsbiz.doctor.entity.DoctorScheduleEntity;

public class DoctorScheduleAction extends AdminBaseAction
{
    private DoctorScheduleDelegate doctorScheduleDelegate;



    public DoctorScheduleDelegate getDoctorScheduleDelegate()
    {
        return doctorScheduleDelegate;
    }

    public void setDoctorScheduleDelegate(DoctorScheduleDelegate doctorScheduleDelegate)
    {
        this.doctorScheduleDelegate = doctorScheduleDelegate;
    }
    private CommonDelegate commonDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public void doResponse(HttpServletRequest request, HttpServletResponse response, JSONArray json)
    {
        try
        {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }
    /**
     * 获取医生排班列表 后台管理界面
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView doctorScheduleList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        params.put("pageNumber", params.getInt("page") == 0 ? 1 : params.getInt("page"));
        params.put("pageSize", params.getInt("rows") == 0 ? 10 : params.getInt("rows"));
        params.put("pageIndex", params.getInt("pageNumber"));
        Object[] result = doctorScheduleDelegate.getDoctorScheduleList(params);
        JSONObject json = new JSONObject();
        json.element("total", (Integer) result[1]);
        json.element("rows", (List) result[0]);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    /**
     * 保存科室信息
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        DoctorScheduleEntity entity = (DoctorScheduleEntity) this.parseRequestParametersToModel(request, DoctorScheduleEntity.class);
        JSONObject json = new JSONObject();
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dd.parse(params.getString("scDate"));
        entity.setScDate(date);
        //校验数据库中是否存在该记录
        int result = doctorScheduleDelegate.saveRec(entity,params);
        if (result == 1) {
            json.element("flag", "success");
            json.element("msg", "保存成功!");
        } else if (result == 0){
            json.element("flag", "fail");
            json.element("msg", "保存失败!");
        } else if(result == 2) {
            json.element("flag", "fail");
            json.element("msg", "该医院科室当天时段已安排人员值班!");
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    /**
     * 根据医院编号和科室编号获取医生下拉列表
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView getDoctorName(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();
        List list = doctorScheduleDelegate.getDoctorName(params);
        if (null != list) {
            for (int i = 0; i < list.size(); i++){
                Row row = (Row) list.get(i);
                JSONObject jo = new JSONObject();
                jo.element("id", row.getString("id"));
                jo.element("name", row.getString("name"));
                json.add(jo);
            }
        }
        this.doResponse(request, response, json);
        return null;
    }

    /** 根据医院编号查询科室列表
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView getSectionName(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();
        List list = doctorScheduleDelegate.getSectionName(params);
        if (null != list) {
            for (int i = 0; i < list.size(); i++){
                Row row = (Row) list.get(i);
                JSONObject jo = new JSONObject();
                jo.element("id", row.getString("id"));
                jo.element("name", row.getString("name"));
                json.add(jo);
            }
        }
        this.doResponse(request, response, json);
        return null;
    } 

    /**
     * 删除排班信息
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        int result = doctorScheduleDelegate.deleteRec(params);
        JSONObject json = new JSONObject();
        if (result > 0)
            json.element("msg", "删除成功!");
        else
            json.element("msg", "删除失败!");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }
    
    /**
     * 取消排班信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView cancelShedule(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Row params = this.parseRequestParametersToRow(request);
        int result = doctorScheduleDelegate.cancelShedule(params);
        JSONObject json = new JSONObject();
        if (result > 0)
            json.element("msg", "取消成功!");
        else
            json.element("msg", "取消失败!");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    /**
     * 编辑排班信息
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        DoctorScheduleEntity entity = doctorScheduleDelegate.getDetailRec(params);
        JSONObject obj = JSONObject.fromObject(entity);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(obj.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }
}
